/*
 * This file is part of Cadmium.
 * Copyright (C) 2007-2010 Xavier Clerc.
 *
 * Cadmium is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Cadmium is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package fr.x9c.cadmium.support;

import fr.x9c.cadmium.kernel.AbstractNativeRunner;
import fr.x9c.cadmium.kernel.Context;
import fr.x9c.cadmium.kernel.Value;
import fr.x9c.cadmium.primitives.cadmium.Cadmium;

/**
 * This class is the parent class of all native applets. <br/>
 * Applet arebased upon the OCaml <i>Graphics</i> library. The applet UI is
 * reduced to a single canvas whose content is controlled by the
 * <i>Graphics</i> library. <br/>
 * <br/>
 * When linked as an applet, the main code is intended to use either
 * <i>Cadmium.register_applet</i> or <i>CadmiumObj.register_applet</i>
 * to register callbacks that will be used for applet methods
 * ({@link java.applet.Applet#init()},
 * {@link java.applet.Applet#start()}, {@link java.applet.Applet#stop()},
 * {@link java.applet.Applet#destroy()}). Additionally, a callback is
 * registered for a <i>run</i> function that is called by the thread this
 * applet launches right after <i>start</i> code. It is thus safe to wait
 * for <i>Graphics</i> events from this <i>run</i> function.
 * The thread executing the <i>run</i> function is killed when the applet is
 * destroyed. <br/>
 * All the callback methods are passed an unique argument that is the actual
 * {@link fr.x9c.cadmium.support.Applet} object. <br/>
 * <br/>
 * One should also notice that the jar file used as an applet archive should
 * be signed, as the Cadmium interpreter relies on reflection. <br/>
 *
 * @author <a href="mailto:cadmium@x9c.fr">Xavier Clerc</a>
 * @version 1.0
 * @since 1.0
 */
public abstract class AbstractNativeApplet extends Applet {

    /** Serialization UID. */
    static final long serialVersionUID = 5446133380529957650L;

    /** Underlying native runner. */
    private AbstractNativeRunner runner;

    /**
     * Empty constructor.
     */
    public AbstractNativeApplet() {
        super();
    } // end empty constructor

    /**
     * {@inheritDoc}
     */
    @Override
    protected final void callback(final String cb) {
        assert cb != null : "null cb";
        try {
            final Value closure = this.runner.getContext().getCallback(cb);
            this.runner.callback(closure, Cadmium.createObject(this));
        } catch (final Throwable t) {
            showStatus(t.toString());
        } // end try/catch
    } // end method 'callback(String)'

    /**
     * {@inheritDoc}
     */
    @Override
    protected final Value getCallback(final String cb) {
        return this.runner.getContext().getCallback(cb);
    } // end method 'getCallback(String)'

    /**
     * {@inheritDoc}
     */
    @Override
    protected final Context getContext() {
        return this.runner.getContext();
    } // end method 'getContext()'

} // end class 'AbstractNativeApplet'
